package com.useeinfo.oa.modules.inspection.biz;

import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.CommonUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.ResourceBiz;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.inspection.dao.InspectionWorkLogDao;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.inspection.entity.InspectionWorkLog;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * @auther: bin.liu
 * @Date: 2019/1/11 15:34
 * @Description: 巡检工单日志service
 */
@Service("inspectionWorkLogBiz")
public class InspectionWorkLogBiz extends CrudBiz<InspectionWorkLogDao, InspectionWorkLog> {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ResourceBiz resourceBiz;

    @Autowired
    private DictInfoBiz dictInfoBiz;

    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;


    public String getCode(String enterpriseId){
        return dao.getCode(enterpriseId);
    }

    /**
     * 添加或修改工作日志
     *
     * @param logId
     * @param logCatId
     * @param logLevelId
     * @param content
     * @param photos
     * @param inspectionOrderId
     * @return
     */
    public ResultDto addOrUpdateLog(String logId, String logCatId, String logLevelId, String content, String photos, String inspectionOrderId) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        if (enterprise == null || systemUser == null) {
            ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }

        InspectionWorkLog inspectionWorkLog;
        if (StringUtils.isNoneBlank(logId)) {
            // 修改
            inspectionWorkLog = dao.findModel(StringConverters.null2Long(logId));
        } else {
            // 添加
            inspectionWorkLog = new InspectionWorkLog();
            inspectionWorkLog.setSerial(CommonUtils.getRandomNumber(6));
            inspectionWorkLog.setCode(dao.getCode(StringConverters.null2String(enterprise.getId())));
        }
        inspectionWorkLog.setContent(StringConverters.null2String(content));
        if (StringUtils.isNoneBlank(photos)) {
            JSONArray jsonArray = JSONArray.parseArray(photos);
            if (jsonArray.size() > 0) {
                Set<Resource> resources = new HashSet<>();
                for (Object id : jsonArray) {
                    Resource resource = resourceBiz.findModel(StringConverters.null2Long(id));
                    resources.add(resource);
                }
                inspectionWorkLog.setLogPhoto(resources);
            }
        }

        inspectionWorkLog.setSystemUser(systemUser);
        inspectionWorkLog.setEnterprise(enterprise);

        if (StringUtils.isNotBlank(logCatId)) {
            DictInfo logCat = dictInfoBiz.findModel(StringConverters.null2Long(logCatId));
            inspectionWorkLog.setLogCat(logCat);
        }

        if (StringUtils.isNotBlank(logLevelId)) {
            DictInfo logLevel = dictInfoBiz.findModel(StringConverters.null2Long(logLevelId));
            inspectionWorkLog.setLogLevel(logLevel);
        }

        if (StringUtils.isNotBlank(inspectionOrderId)) {
            InspectionOrder inspectionOrder = inspectionOrderBiz.findModel(StringConverters.null2Long(inspectionOrderId));
            inspectionWorkLog.setInspectionOrder(inspectionOrder);
        }
        addOrUpdate(inspectionWorkLog);
        return ResultUtil.success();
    }

    /**
     * 工作日志列表
     *
     * @param pageIndex
     * @param pageSize
     * @param inspectionOrderId
     * @return
     */
    public ResultDto inspectionWorkLogList(String pageIndex, String pageSize, String inspectionOrderId) {
        QueryParam queryParam = new QueryParam(pageIndex, pageSize);
        if (StringUtils.isNotBlank(inspectionOrderId)) {
            queryParam.getSqlMap().put("inspectionOrderId", inspectionOrderId);
        }
        return ResultUtil.success(findJSONList(queryParam));
    }

    /**
     * 工作日志详情
     *
     * @param inspectionWorkLogId
     * @return
     */
    public ResultDto inspectionWorkLogDetail(String inspectionWorkLogId) {
        if (StringUtils.isBlank(inspectionWorkLogId)) {
            return ResultUtil.error("参数错误");
        }
        InspectionWorkLog inspectionWorkLog = findModel(StringConverters.null2Long(inspectionWorkLogId));

        return ResultUtil.success(inspectionWorkLog.toJSONObjectDetail());
    }
}
